package com.atguigu.srb.core.service.impl;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.enums.UserBindEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.mapper.UserBindMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.UserBindVO;
import com.atguigu.srb.core.service.UserBindService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author Bright
 * @since 2022-04-09
 */
@Service
@Slf4j
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

    @Resource
    UserInfoMapper userInfoMapper;

    @Override
    public String commitBindUser(UserBindVO userBindVO, Long userId) {
        //查询身份证号码是否绑定
        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper
                .eq("id_card", userBindVO.getIdCard())
                .ne("user_id", userId);
        Integer count = baseMapper.selectCount(userBindQueryWrapper);
        //USER_BIND_IDCARD_EXIST_ERROR(-301, "身份证号码已绑定"),
        Assert.isTrue(count == 0, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);
        //查询用户绑定信息
        QueryWrapper<UserBind> userBindQueryWrapper1 = new QueryWrapper<>();
        userBindQueryWrapper1.eq("user_id", userId);
        Integer bindCount = baseMapper.selectCount(userBindQueryWrapper1);
        UserBind userBind = new UserBind();
        BeanUtils.copyProperties(userBindVO, userBind);
        userBind.setUserId(userId);
        //判断是否有绑定记录
        if (bindCount > 0) {
            baseMapper.updateById(userBind);
        } else {
            //如果未创建绑定记录，则创建一条记录
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            baseMapper.insert(userBind);
        }
        //曾经跳转到托管平台，但是未操作完成，此时将用户最新填写的数据同步到userBind对象
        //构建充值自动提交表单
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("agent_id", HfbConst.AGENT_ID);
        paramMap.put("agentUserId", userBind.getUserId());
        paramMap.put("idCard", userBind.getIdCard());
        paramMap.put("personalName", userBind.getName());
        paramMap.put("bankType", userBind.getBankType());
        paramMap.put("bankNo", userBind.getBankNo());
        paramMap.put("mobile", userBind.getMobile());
        paramMap.put("returnUrl", HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
        paramMap.put("timestamp",  RequestHelper.getTimestamp());
        paramMap.put("sign", RequestHelper.getSign(paramMap));

        log.info(userBind.toString());

        return FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);
    }

    @Override
    public void notify(Map<String, Object> paramMap) {
        //会员id
        String agentUserId = (String) paramMap.get("agentUserId");
        String bindCode = (String) paramMap.get("bindCode");
        //根据user_id查询user_bind记录
        UpdateWrapper<UserBind> userBindUpdateWrapper = new UpdateWrapper<>();
        userBindUpdateWrapper.eq("user_id", agentUserId);
        //更新用户绑定表
        UserBind userBind = new UserBind();
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        userBind.setBindCode(bindCode);
        baseMapper.update(userBind, userBindUpdateWrapper);
        //更新用户表
        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper.eq("user_id", agentUserId);
        userBind = baseMapper.selectOne(userBindQueryWrapper);

        UserInfo userInfo = new UserInfo();
        userInfo.setId(userBind.getUserId());
        userInfo.setName(userBind.getName());
        userInfo.setNickName(userBind.getName());
        userInfo.setBindCode(bindCode);
        userInfo.setIdCard(userBind.getIdCard());
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());

        userInfoMapper.updateById(userInfo);
    }


}
